package signGrammar.view;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.JComboBox;
import javax.swing.JPanel;

public class CViewPackageChooser extends JPanel{
	private static final long serialVersionUID = 1L;
	
	public interface IPackageChooserListener{
		public void packageChoosed(String packageName);
	}
	
	private class CPackageChooserPublisher implements ActionListener{
		@Override public void actionPerformed(ActionEvent a) {
			for( IPackageChooserListener listener : packageListeners )
				listener.packageChoosed( comboBox.getSelectedItem().toString() );
		}
	}
	
	public CViewPackageChooser() {
		initComponents();
	}
	
	private JComboBox comboBox;
	private JPanel mainContent;
	private void initComponents() {
		mainContent = new JPanel(new BorderLayout());
		setLayout(new BorderLayout());
		add( mainContent, "Center" );
	}
	
	public CViewPackageChooser setChoises(String[] packages){
		comboBox = new JComboBox( packages );
		mainContent.add(comboBox,"North");
		comboBox.addActionListener( new CPackageChooserPublisher() );
		return this;
	}
	
	protected ArrayList<IPackageChooserListener> packageListeners = new ArrayList<IPackageChooserListener>();
	public CViewPackageChooser addPackageListener(IPackageChooserListener listener){
		packageListeners.add( listener );
		return this;
	}
	public boolean removePackageListener(IPackageChooserListener listener){
		return packageListeners.remove( listener );
	}
}
